package file;

import java.nio.CharBuffer;

/**
 * 交换两个buffer中的字符
 */
public class Test2 {
    static void swap(CharBuffer buffer) {
        while(buffer.hasRemaining()) {
            buffer.mark();
            char c1 = buffer.get();
            char c2 = buffer.get();
            buffer.reset();
            buffer.put(c2).put(c1);
        }
    }

    public static void main(String[] args) {
        char[] chars = "Hello, World!!".toCharArray();
        CharBuffer buffer = CharBuffer.allocate(chars.length);
        buffer.put(chars);
        System.out.println(buffer.rewind());

        swap(buffer);
        System.out.println(buffer.rewind());

        swap(buffer);
        System.out.println(buffer.rewind());
    }
}
